//
//  NMNMSGSCViewController.m
//  HNJYB
//
//  Created by Mag1cPanda on 2017/3/23.
//  Copyright © 2017年 Mag1cPanda. All rights reserved.
//

#import "NMNMSGSCViewController.h"
#import "NMSGSCGroup.h"
#import "IDyz.h"
#import "NMNMZRRDViewController.h"

@interface NMNMSGSCViewController ()
< UITextFieldDelegate >
{
    UIScrollView *scroll;
    
    NMSGSCGroup *jfGroup;
    NMSGSCGroup *yfGroup;
    
}
@end

@implementation NMNMSGSCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"事故上传";
    
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    [self.view addSubview:scroll];
    
    jfGroup = [[NMSGSCGroup alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 440) groupId:@"jfGroup"];
    jfGroup.title = @"甲方信息";
    jfGroup.brandView.field.delegate = self;
    jfGroup.nameView.field.delegate = self;
    jfGroup.phoneView.field.delegate = self;
    [scroll addSubview:jfGroup];
    
    yfGroup = [[NMSGSCGroup alloc] initWithFrame:CGRectMake(0, jfGroup.maxY, ScreenWidth, 440) groupId:@"nmGroup"];
    yfGroup.title = @"乙方信息";
    yfGroup.brandView.field.delegate = self;
    yfGroup.nameView.field.delegate = self;
    yfGroup.phoneView.field.delegate = self;
    [scroll addSubview:yfGroup];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, yfGroup.maxY + 25, ScreenWidth - 20, 50);
    [btn setTitle:@"提交" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(confirmBtnClicked) forControlEvents:1 << 6];
    btn.backgroundColor = HNBlue;
    btn.layer.cornerRadius = 5;
    [scroll addSubview:btn];
    
    scroll.contentSize = CGSizeMake(ScreenWidth, btn.maxY + 20);
    
    [self ocrAutofill];
}

- (void)ocrAutofill
{
    NSMutableDictionary *ocrBean = [NSMutableDictionary dictionary];
    [ocrBean setValue:GlobleInstance.userflag forKey:@"userflag"];
    [ocrBean setValue:GlobleInstance.token forKey:@"token"];
    [ocrBean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    
    MBProgressHUD *ocrHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappgetOCRdata" parameters:ocrBean complete:^(id result, ResultType resultType) {
        
        [ocrHud hideAnimated:YES];
        NSLog(@"jjappgetOCRdata ~ %@",result);
        NSArray *dataArr = result[@"data"];
        
        for (NSDictionary *dic in dataArr) {
            //甲方身份证正面
            if ([dic[@"imagetype"] isEqual:@"16"])
            {
                GlobleInstance.nmjfocrname = dic[@"ocrname"];
                GlobleInstance.nmjfocrcardno = dic[@"ocrcardno"];
            }
            //甲方身份证反面无需识别
            if ([dic[@"imagetype"] isEqual:@"17"])
            {
                
            }
            //乙方身份证正面
            if ([dic[@"imagetype"] isEqual:@"18"]) {
                GlobleInstance.nmyfocrname = dic[@"ocrname"];
                GlobleInstance.nmyfocrcardno = dic[@"ocrcardno"];
            }
            //乙方身份证反面无需识别
            if ([dic[@"imagetype"] isEqual:@"19"]) {
                
            }
        }
        
        /**********************************************/
        
        //填充甲方
        jfGroup.nameView.field.text = GlobleInstance.nmjfocrname;
        jfGroup.idcardView.field.text = GlobleInstance.nmjfocrcardno;
        
        //填充乙方
        yfGroup.nameView.field.text = GlobleInstance.nmyfocrname;
        yfGroup.idcardView.field.text = GlobleInstance.nmyfocrcardno;
        
    }];
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //甲方身份证号
    if (textField == jfGroup.idcardView.field)
    {
        textField.text = [textField.text uppercaseString];
        
    }
    //乙方身份证号
    if (textField == yfGroup.idcardView.field)
    {
        textField.text = [textField.text uppercaseString];
    }
    
}

#pragma mark - 提交按钮点击事件
- (void)confirmBtnClicked
{
    //取值
    //甲方
    NSString *jfbrand = jfGroup.brandView.field.text;
    NSString *jfcartype = jfGroup.selectedIndex;
    NSString *jfname = jfGroup.nameView.field.text;
    NSString *jfphoneno = jfGroup.phoneView.field.text;
    NSString *jfidno = jfGroup.idcardView.field.text;
    
    //乙方
    NSString *yfbrand = yfGroup.brandView.field.text;
    NSString *yfcartype = yfGroup.selectedIndex;
    NSString *yfname = yfGroup.nameView.field.text;
    NSString *yfphoneno = yfGroup.phoneView.field.text;
    NSString *yfidno = yfGroup.idcardView.field.text;
    
    //非机动车甲方Judge
    if (jfbrand.length < 1)
    {
        SHOWALERT(@"请填写甲方车辆品牌");
        return;
    }
    
    if (!jfcartype)
    {
        SHOWALERT(@"请选择甲方车型");
        return;
    }
    
    if (jfname.length < 1)
    {
        SHOWALERT(@"请填写甲方姓名");
        return;
    }
    
    if (![Util isMobileNumber:jfphoneno])
    {
        SHOWALERT(@"请填写正确的甲方联系电话");
        return;
    }
    
    if (![IDyz validateIDCardNumber:jfidno])
    {
        SHOWALERT(@"请填写正确的甲方身份证号");
        return;
    }
    
    //非机动车乙方Judge
    if (yfbrand.length < 1)
    {
        SHOWALERT(@"请填写乙方车辆品牌");
        return;
    }
    
    if (!yfcartype)
    {
        SHOWALERT(@"请选择乙方车型");
        return;
    }
    
    if (yfname.length < 1)
    {
        SHOWALERT(@"请填写乙方姓名");
        return;
    }
    
    if (![Util isMobileNumber:yfphoneno])
    {
        SHOWALERT(@"请填写正确的乙方联系电话");
        return;
    }
    
    if (![IDyz validateIDCardNumber:yfidno])
    {
        SHOWALERT(@"请填写正确的乙方身份证号");
        return;
    }
    
    
    //判断双方是否相同
    if ([jfphoneno isEqualToString:yfphoneno])
    {
        SHOWALERT(@"双方手机号不能相同");
        return;
    }
    
    if ([jfidno isEqualToString:yfidno])
    {
        SHOWALERT(@"双方证件号不能相同");
        return;
    }
    
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    
    //casecarno字段存车辆品牌
    NSArray *arr = @[@{@"carownname":jfname,
                       @"carownphone":jfphoneno,
                       @"casecarno":jfbrand,
                       @"cartype":jfcartype,
                       @"frameno":@"",
                       @"cardno":jfidno,
                       @"driverlicence":@"",
                       @"party":@"1"},
                     @{@"carownname":yfname,
                       @"carownphone":yfphoneno,
                       @"casecarno":yfbrand,
                       @"cartype":yfcartype,
                       @"frameno":@"",
                       @"cardno":yfidno,
                       @"driverlicence":@"",
                       @"party":@"0"}];
    
    NSString *appcaseno = GlobleInstance.appcaseno;
    
    [bean setValue:arr forKey:@"casecarlist"];
    [bean setValue:appcaseno forKey:@"appcaseno"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappcheckcbxx" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        
        if (result) {
            NSLog(@"承保信息 ~ %@",[Util objectToJson:result]);
        }
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            NMNMZRRDViewController *vc = [NMNMZRRDViewController new];
            
            InfoModel *jfModel = [InfoModel new];
            jfModel.carType = jfcartype;
            jfModel.name = jfname;
            jfModel.carNum = jfbrand;//非机动车carno存车辆品牌
            jfModel.phoneNum = jfphoneno;
            jfModel.driveNum = jfidno;
            jfModel.vinNum = @"";//字段没有留空
            jfModel.bdhNum = @"";//字段没有留空
            
            InfoModel *yfModel = [InfoModel new];
            yfModel.carType = yfcartype;
            yfModel.name = yfname;
            yfModel.carNum = yfbrand;//非机动车carno存车辆品牌
            yfModel.phoneNum = yfphoneno;
            yfModel.driveNum = yfidno;
            yfModel.vinNum = @"";//字段没有留空
            yfModel.bdhNum = @"";//字段没有留空
            
            vc.jfModel = jfModel;
            vc.yfModel = yfModel;
            vc.selectedItem = _selectedItem;
            vc.caseDes = _caseDes;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        else {
            SHOWALERT(result[@"redes"]);
        }
        
    }];
    
}


@end
